package com.yida.gwj.deposit.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yida.common.core.constant.SecurityConstants;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.core.utils.PageUtils;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.redis.service.RedisService;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.api.domain.GwjConfStep;
import com.yida.gwj.api.domain.GwjFinanceAccountMain;
import com.yida.gwj.api.service.RemoteGwjConfigService;
import com.yida.gwj.api.service.RemoteGwjFinanceService;
import com.yida.gwj.common.constant.GwjCacheConstants;
import com.yida.gwj.common.constant.GwjConstants;
import com.yida.gwj.common.domain.GwjBaseBuild;
import com.yida.gwj.common.domain.GwjBaseBusinessBuild;
import com.yida.gwj.common.domain.GwjBaseBusinessNum;
import com.yida.gwj.common.domain.GwjBaseCommunity;
import com.yida.gwj.common.domain.GwjBaseHouse;
import com.yida.gwj.common.domain.GwjBaseOwner;
import com.yida.gwj.common.domain.dto.GwjUserDTO;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.common.enums.GwjBusinessHouseStatus;
import com.yida.gwj.common.enums.GwjBusinessStatus;
import com.yida.gwj.common.enums.GwjCheckAccountStatus;
import com.yida.gwj.common.service.IGwjBaseBuildService;
import com.yida.gwj.common.service.IGwjBaseBusinessNumService;
import com.yida.gwj.common.service.IGwjBaseCommunityService;
import com.yida.gwj.common.service.IGwjBaseHouseService;
import com.yida.gwj.common.service.IGwjBaseOwnerService;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.deposit.domain.GwjDepositBusiness;
import com.yida.gwj.deposit.domain.GwjDepositBusinessBuild;
import com.yida.gwj.deposit.domain.GwjDepositBusinessCommunity;
import com.yida.gwj.deposit.domain.GwjDepositBusinessDetail;
import com.yida.gwj.deposit.domain.GwjDepositBusinessHouse;
import com.yida.gwj.deposit.domain.GwjDepositBusinessLog;
import com.yida.gwj.deposit.domain.GwjDepositBusinessMoney;
import com.yida.gwj.deposit.domain.dto.GwjDepositBusinessDetailDTO;
import com.yida.gwj.deposit.domain.dto.GwjDepositCommunityDetailDTO;
import com.yida.gwj.deposit.domain.dto.GwjDepositMoneyDetailDTO;
import com.yida.gwj.deposit.domain.dto.GwjDepositPageDTO;
import com.yida.gwj.deposit.domain.dto.GwjDepositPaymentDetailDTO;
import com.yida.gwj.deposit.domain.dto.GwjOwnerDepositBuildDetailDTO;
import com.yida.gwj.deposit.domain.dto.GwjOwnerDepositDetailDTO;
import com.yida.gwj.deposit.domain.dto.GwjOwnerDepositHouseDetailDTO;
import com.yida.gwj.deposit.domain.request.GwjDepositBusinessBuildRequest;
import com.yida.gwj.deposit.domain.request.GwjDepositBusinessCommunityRequest;
import com.yida.gwj.deposit.domain.request.GwjDepositBusinessDetailRequest;
import com.yida.gwj.deposit.domain.request.GwjDepositBusinessHouseRequest;
import com.yida.gwj.deposit.domain.request.GwjDepositBusinessLogRequest;
import com.yida.gwj.deposit.domain.request.GwjDepositBusinessMoneyRequest;
import com.yida.gwj.deposit.domain.request.GwjDepositBusinessRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositBusinessBuildRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositBusinessHouseRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositChooseBuildRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositChooseCommunityRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositChooseHouseRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositEditMoneyRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositSaveSubmitRequest;
import com.yida.gwj.deposit.enums.GwjDepositStatusEnum;
import com.yida.gwj.deposit.enums.GwjOwnerDepositStepEnum;
import com.yida.gwj.deposit.mapper.GwjDepositBusinessMapper;
import com.yida.gwj.deposit.service.IGwjDepositBusinessBuildService;
import com.yida.gwj.deposit.service.IGwjDepositBusinessCommunityService;
import com.yida.gwj.deposit.service.IGwjDepositBusinessDetailService;
import com.yida.gwj.deposit.service.IGwjDepositBusinessHouseService;
import com.yida.gwj.deposit.service.IGwjDepositBusinessLogService;
import com.yida.gwj.deposit.service.IGwjDepositBusinessMoneyService;
import com.yida.gwj.deposit.service.IGwjDepositBusinessService;
import com.yida.gwj.deposit.service.IGwjOwnerDepositBusinessService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;

/**
 * @Description 业主缴存（缴存业务管理模块）Service业务层处理
 * @Author wws
 * @Date 2023-02-14
 */
@Service
public class GwjOwnerDepositBusinessServiceImpl implements IGwjOwnerDepositBusinessService {

    @Resource
    private RemoteGwjConfigService remoteGwjConfigService;

    @Resource
    private IGwjBaseBusinessNumService businessNumService;

    @Resource
    private IGwjDepositBusinessService gwjDepositBusinessService;

    @Resource
    private IGwjDepositBusinessCommunityService gwjDepositBusinessCommunityService;

    @Resource
    private IGwjDepositBusinessBuildService gwjDepositBusinessBuildService;

    @Resource
    private IGwjBaseHouseService houseService;

    @Resource
    private IGwjDepositBusinessHouseService gwjDepositBusinessHouseService;

    @Resource
    private IGwjDepositBusinessMoneyService gwjDepositBusinessMoneyService;

    @Resource
    private IGwjDepositBusinessLogService gwjDepositBusinessLogService;

    @Resource
    private RemoteGwjFinanceService remoteGwjFinanceService;

    @Resource
    private GwjDepositBusinessMapper gwjDepositBusinessMapper;

    @Resource
    private IGwjDepositBusinessDetailService gwjDepositBusinessDetailService;

    @Resource
    private IGwjBaseBuildService buildService;

    @Resource
    private RedisService redisService;

    @Resource
    IGwjBaseCommunityService communityService;

    @Autowired
    private IGwjBaseOwnerService baseOwnerService;

    /**
     * 业主缴存(保存)
     *
     * @param saveSubmitRequest 保存参数
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveBusiness(GwjOwnerDepositSaveSubmitRequest saveSubmitRequest) {
        GwjDepositBusinessRequest businessRequest = saveSubmitRequest.getBusiness();
        GwjDepositBusinessCommunityRequest communityRequest = saveSubmitRequest.getBusinessCommunity();
        List<GwjDepositBusinessBuildRequest> buildsRequest = saveSubmitRequest.getBusinessBuilds();
        List<GwjDepositBusinessHouseRequest> housesRequest = saveSubmitRequest.getBusinessHouses();
        GwjDepositBusinessMoneyRequest moneyRequest = saveSubmitRequest.getBusinessMoney();

        // 查询基础参数
        GwjBaseCommunity gwjBaseCommunity = communityService.selectGwjBaseCommunityByGwjBaseCommunityId(communityRequest.getCommunityId());
        List<GwjBaseBuild> gwjBaseBuilds = buildService.selectGwjBaseBuildByBuildIdList(buildsRequest.stream().map(GwjDepositBusinessBuildRequest::getBuildId).toArray(Long[]::new));
        List<GwjBaseHouse> gwjBaseHouses = houseService.selectGwjBaseHoseByHouseIds(housesRequest.stream().map(GwjDepositBusinessHouseRequest::getHouseId).toArray(Long[]::new));
        // 拷贝基础参数
        GwjDepositBusiness business = new GwjDepositBusiness();
        GwjDepositBusinessCommunity community = new GwjDepositBusinessCommunity();
        GwjDepositBusinessMoney money = new GwjDepositBusinessMoney();

        BeanUtil.copyProperties(gwjBaseCommunity, community);
        List<GwjDepositBusinessBuild> builds = gwjBaseBuilds.stream().map(baseBuild -> {
            GwjDepositBusinessBuild gwjDepositBusinessBuild = new GwjDepositBusinessBuild();
            BeanUtil.copyProperties(baseBuild, gwjDepositBusinessBuild);
            return gwjDepositBusinessBuild;
        }).collect(Collectors.toList());
        List<GwjDepositBusinessHouse> houses = gwjBaseHouses.stream().map(baseHouse -> {
            GwjDepositBusinessHouse gwjDepositBusinessHouse = new GwjDepositBusinessHouse();
            BeanUtil.copyProperties(baseHouse, gwjDepositBusinessHouse);
            return gwjDepositBusinessHouse;
        }).collect(Collectors.toList());
        // 拷贝业务参数
        BeanUtil.copyProperties(moneyRequest, money);
        BeanUtil.copyProperties(businessRequest, business);
        BeanUtil.copyProperties(communityRequest, community);
        builds = builds.stream().map(build -> {
            buildsRequest.forEach(buildRequest -> {
                if (build.getBuildId().equals(buildRequest.getBuildId())) {
                    BeanUtil.copyProperties(buildRequest, build);
                }
            });
            return build;
        }).collect(Collectors.toList());
        houses = houses.stream().map(house -> {
            housesRequest.forEach(houseRequest -> {
                if (house.getHouseId().equals(houseRequest.getHouseId())) {
                    BeanUtil.copyProperties(houseRequest, house);
                    house.setLackMoney(houseRequest.getDepositMoney());
                }
            });
            house.setSelectFlag(GwjBusinessHouseStatus.CONFIRM.getCode());
            return house;
        }).collect(Collectors.toList());

        // 合计缴存金额
        BigDecimal totalDepositMoney = new BigDecimal("0.00");
        for (GwjDepositBusinessHouse house : houses) {
            totalDepositMoney = totalDepositMoney.add(house.getLackMoney());
        }
        // 处理业务信息
        // 设置差额
        business.setLackMoney(totalDepositMoney);
        // 设置步骤
        business.setStepId(GwjOwnerDepositStepEnum.ACCEPTED.getStepId());
        // 业务流程
        business.setFlowId(remoteGwjConfigService.selectGwjConfBusinessFlagByBizFlagId(business.getBizFlagId(), SecurityConstants.INNER).getData().getFlowId());
        // 设置地区
        business.setAreaId(community.getAreaId());
        // TODO 获取业务编号加锁
        GwjBaseBusinessNum businessNum = businessNumService.getNextNum(business.getAreaId(), business.getBizFlagId());
        // 业务编号
        business.setBizNum(Convert.toLong(businessNum));
        // 保存业务信息
        gwjDepositBusinessService.insertGwjDepositBusiness(business);

        // 处理业务小区信息
        // 设置业务主键
        community.setBizId(business.getBizId());
        // 保存小区信息
        gwjDepositBusinessCommunityService.insertGwjDepositBusinessCommunity(community);

        // 处理业务楼栋信息
        // 处理楼栋信息
        builds = builds.stream().map(build -> {
            build.setBizId(community.getBizId());
            build.setAreaId(community.getAreaId());
            build.setStreetId(community.getStreetId());
            build.setBizCommunityId(community.getBizCommunityId());
            return build;
        }).collect(Collectors.toList());
        // 批量保存楼栋信息
        if (ObjectUtil.isEmpty(builds)) {
            throw new ServiceException("楼栋信息不能为空");
        }
        gwjDepositBusinessBuildService.insertGwjDepositBusinessBuilds(builds);

        // 处理业务房屋信息
        // 查询房屋基础信息
        String[] buildCodes = builds.stream().map(GwjDepositBusinessBuild::getBuildCode).toArray(String[]::new);
        List<GwjBaseHouse> baseHouses = houseService.selectGwjBaseListByBuildCodesAndAreaId(buildCodes, community.getAreaId());
        BigDecimal totalMoney = new BigDecimal("0.00");
        BigDecimal totalSize = new BigDecimal("0.00");
        BigDecimal factSize = new BigDecimal("0.00");
        Long totalOwner = new Long(0L);
        // 处理房屋信息
        List<GwjDepositBusinessHouse> houseList = new ArrayList<GwjDepositBusinessHouse>(baseHouses.size());
        for (GwjDepositBusinessHouse businessHouse : houses) {
            businessHouse.setBizId(community.getBizId());
            businessHouse.setAreaId(community.getAreaId());
            businessHouse.setStreetId(community.getStreetId());
            totalMoney = totalMoney.add(businessHouse.getDepositMoney());
            totalSize = totalSize.add(businessHouse.getForecastSize());
            houseList.add(businessHouse);
            factSize = factSize.add(businessHouse.getActualSize());
            totalOwner += 1;
        }
        // 批量信息房屋信息
        if (ObjectUtil.isEmpty(houseList)) {
            throw new ServiceException("房屋信息不能为空");
        }
        gwjDepositBusinessHouseService.insertGwjDepositBusinessHouses(houseList);

        // 处理业务金额信息
        money.setTotalMoney(totalMoney);
        money.setTotalSize(totalSize);
        money.setFactSize(factSize);
        money.setTotalOwner(totalOwner);
        money.setBizId(business.getBizId());
        money.setLedgerType(GwjConstants.INCOME);
        money.setAreaId(community.getAreaId());
        // 保存业务金额信息
        gwjDepositBusinessMoneyService.insertGwjDepositBusinessMoney(money);

        // 保存日志
        gwjDepositBusinessLogService.addSaveLog(business);
        return business.getBizId();
    }

    /**
     * 业主缴存（签署意见）
     *
     * @param gwjDepositBusinessLog 意见请求参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void signOpinion(GwjDepositBusinessLog gwjDepositBusinessLog) {
        // 判断当前用户是否有权限
        GwjDepositBusiness gwjDepositBusiness = gwjDepositBusinessService.checkPermission(gwjDepositBusinessLog.getBizId());
        // 记录日志
        gwjDepositBusinessLog.setStepId(gwjDepositBusiness.getStepId());
        gwjDepositBusinessLogService.addSignOpinionLog(gwjDepositBusinessLog);
    }

    /**
     * 业主缴存（回退）
     *
     * @param businessLog 回退参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void fallback(GwjDepositBusinessLog businessLog) {
        // 判断当前用户是否有权限
        GwjDepositBusiness gwjDepositBusiness = gwjDepositBusinessService.checkPermission(businessLog.getBizId());
        // 查找上一个步骤办理人员
        GwjConfStep step = remoteGwjConfigService.selectPreviousGwjConfStepByFlagId(gwjDepositBusiness.getBizFlagId(), gwjDepositBusiness.getStepId(), SecurityConstants.INNER).getData();
        if (StringUtils.isNull(step)) {
            throw new ServiceException("当前业务步骤是第一步，不能再回退");
        }
        // 设置日志的当前步骤
        businessLog.setStepId(gwjDepositBusiness.getStepId());
        // 设置业务回退的步骤
        gwjDepositBusiness.setStepId(step.getStepId());
        // 修改业务当前办理人员为上一个流程的最终提交人
        GwjUserDTO user = gwjDepositBusinessLogService.getPreviousSubmitUser(gwjDepositBusiness.getBizId(), step.getStepId());
        gwjDepositBusiness.setCurrentOperId(user.getUserId());
        gwjDepositBusiness.setCurrentOperName(user.getRealName());
        // 设置回退状态
        gwjDepositBusiness.setStatus(GwjBusinessStatus.FALLBACK.getCode());
        gwjDepositBusinessMapper.updateGwjDepositBusiness(gwjDepositBusiness);
        // 保存回退日志
        businessLog.setRealName(user.getRealName());
        businessLog.setUserId(user.getUserId());
        // 重置总账、分账流水
        if (!gwjDepositBusinessService.removeLedger(businessLog.getBizId())) {
            throw new ServiceException("总账、分账流水重置失败");
        }
        gwjDepositBusinessLogService.addFallbackLog(businessLog);
    }

    /**
     * 业主缴存（业务作废）
     *
     * @param gwjDepositBusinessLog 业务作废参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void invalidBusiness(GwjDepositBusinessLog gwjDepositBusinessLog) {
        // 判断当前用户是否有权限
        GwjDepositBusiness gwjDepositBusiness = gwjDepositBusinessService.checkPermission(gwjDepositBusinessLog.getBizId());
        // 设置日志的当前步骤
        gwjDepositBusinessLog.setStepId(gwjDepositBusiness.getStepId());
        // 设置作废状态
        gwjDepositBusiness.setStatus(GwjBusinessStatus.INVALID.getCode());
        // 设置业务步骤为最后一步案例回顾
        List<GwjConfStep> steps = remoteGwjConfigService.selectGwjConfStepListByFlagId(gwjDepositBusiness.getBizFlagId(), SecurityConstants.INNER).getData();
        gwjDepositBusiness.setStepId(steps.get(steps.size() - 1).getStepId());
        gwjDepositBusinessMapper.updateGwjDepositBusiness(gwjDepositBusiness);
        // 添加日志
        gwjDepositBusinessLogService.addInvalidBusinessLog(gwjDepositBusinessLog);
    }

    /**
     * 业主缴费（送件）
     *
     * @param businessLog 送件参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void send(GwjDepositBusinessLog businessLog) {
        // 判断当前用户是否有权限
        GwjDepositBusiness gwjDepositBusiness = gwjDepositBusinessService.checkPermission(businessLog.getBizId());
        // 修改当前办理人员
        gwjDepositBusiness.setCurrentOperId(businessLog.getUserId());
        gwjDepositBusiness.setCurrentOperName(businessLog.getRealName());
        gwjDepositBusinessMapper.updateGwjDepositBusiness(gwjDepositBusiness);
        // 记录日志
        gwjDepositBusinessLogService.addSendLog(businessLog, gwjDepositBusiness);

    }

    /**
     * 业主缴存（生成流水）
     *
     * @param bizId 业务主键
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void genFinanceStatement(Long bizId) {
        // 验证用户是否拥有操作权限
        gwjDepositBusinessService.checkPermission(bizId);
        // 重置总账、分账流水
        if (!gwjDepositBusinessService.removeLedger(bizId)) {
            throw new ServiceException("总账、分账流水重置失败");
        }
        // 生成总账、分账流水
        if (!gwjDepositBusinessService.generateLedger(bizId)) {
            throw new ServiceException("生成总账、分账流水失败");
        }
    }

    /**
     * 业务缴存（财务对账）
     *
     * @param bizId 业务主键
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void financeCheck(Long bizId) {
        // 验证是否有操作权限
        gwjDepositBusinessService.checkPermission(bizId);
        GwjDepositBusinessMoney money = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(bizId);
        List<GwjDepositBusinessDetail> details = gwjDepositBusinessDetailService.selectGwjDepositBusinessDetailByBizId(bizId);
        // 判断业务金额信息
        if (ObjectUtil.isEmpty(money)) {
            throw new ServiceException("查无金额数据");
        }
        // 判断缴存明细
        if (ObjectUtil.isEmpty(details)) {
            throw new ServiceException("查无对应缴存明细");
        }
        // 校验缴存信息
        if (!money.getTotalMoney().equals(money.getFactMoney())) {
            throw new ServiceException("对账失败，实缴金额与应交金额不相等，请重新核对");
        }
        // 合计缴存明细已交金额
        BigDecimal detailTotalDepositMoney = new BigDecimal("0.00");
        for (GwjDepositBusinessDetail detail : details) {
            detailTotalDepositMoney = detailTotalDepositMoney.add(detail.getDepositMoney());
        }
        // 校验缴存明细
        if (!money.getFactMoney().equals(detailTotalDepositMoney)) {
            throw new ServiceException("对账失败，实缴金额与已交金额总数不相等，请重新核对");
        }
        // 修改业务金额缴费状态
        money.setDepositStatus(GwjDepositStatusEnum.YES.getCode());
        money.setStatus(GwjCheckAccountStatus.CHECK_SUCCESS.getCode());
        gwjDepositBusinessMoneyService.updateGwjDepositBusinessMoney(money);
        // 修改总账、分账对账状态
        if (!gwjDepositBusinessService.edisLedgerStatus(bizId, money.getStatus())) {
            throw new ServiceException("总账、分账对账状态修改失败");
        }
    }

    /**
     * 业主缴存（修改业务信息）
     *
     * @param saveSubmitRequest 修改参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(GwjOwnerDepositSaveSubmitRequest saveSubmitRequest) {
        // 处理业务参数
        GwjDepositBusinessRequest businessRequest = saveSubmitRequest.getBusiness();
        // GwjDepositBusinessCommunityRequest communityRequest = saveSubmitRequest.getBusinessCommunity();
        // List<GwjDepositBusinessBuildRequest> buildsRequest = saveSubmitRequest.getBusinessBuilds();
        // List<GwjDepositBusinessHouseRequest> housesRequest = saveSubmitRequest.getBusinessHouses();
        GwjDepositBusinessMoneyRequest moneyRequest = saveSubmitRequest.getBusinessMoney();
        List<GwjDepositBusinessDetailRequest> detailsRequest = saveSubmitRequest.getDetails();
        // // 查询基础参数
        // GwjBaseCommunity gwjBaseCommunity =
        // communityService.selectGwjBaseCommunityByGwjBaseCommunityId(communityRequest.getCommunityId());
        // List<GwjBaseBuild> gwjBaseBuilds =
        // buildService.selectGwjBaseBuildByBuildIdList(buildsRequest.stream().map(GwjDepositBusinessBuildRequest::getBuildId).toArray(Long[]::new));
        // List<GwjBaseHouse> gwjBaseHouses =
        // houseService.selectGwjBaseHoseByHouseIds(housesRequest.stream().map(GwjDepositBusinessHouseRequest::getHouseId).toArray(Long[]::new));
        // // 拷贝基础参数
        // GwjDepositBusinessCommunity community = new GwjDepositBusinessCommunity();
        // List<GwjDepositBusinessBuild> builds = gwjBaseBuilds.stream().map(baseBuild -> {
        // GwjDepositBusinessBuild gwjDepositBusinessBuild = new GwjDepositBusinessBuild();
        // BeanUtil.copyProperties(baseBuild, gwjDepositBusinessBuild);
        // return gwjDepositBusinessBuild;
        // }).collect(Collectors.toList());
        // List<GwjDepositBusinessHouse> houses = gwjBaseHouses.stream().map(baseHouse -> {
        // GwjDepositBusinessHouse gwjDepositBusinessHouse = new GwjDepositBusinessHouse();
        // BeanUtil.copyProperties(baseHouse, gwjDepositBusinessHouse);
        // return gwjDepositBusinessHouse;
        // }).collect(Collectors.toList());
        // BeanUtil.copyProperties(gwjBaseCommunity, community);
        // 拷贝请求参数
        GwjDepositBusiness business = new GwjDepositBusiness();
        GwjDepositBusinessMoney money = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(businessRequest.getBizId());
        BeanUtil.copyProperties(businessRequest, business);
        // BeanUtil.copyProperties(communityRequest, community);
        // community.setBizId(business.getBizId());
        BeanUtil.copyProperties(moneyRequest, money);
        List<GwjDepositBusinessDetail> details = new ArrayList<GwjDepositBusinessDetail>();
        if (ObjectUtil.isNotEmpty(detailsRequest)) {
            details = detailsRequest.stream().map(detailRequest -> {
                GwjDepositBusinessDetail detail = new GwjDepositBusinessDetail();
                BeanUtil.copyProperties(detailRequest, detail);
                detail.setDepositSize(new BigDecimal(detailRequest.getDepositSize()));
                return detail;
            }).collect(Collectors.toList());
        }
        // builds = builds.stream().map(build -> {
        // buildsRequest.forEach(buildRequest -> {
        // if (build.getBuildId().equals(buildRequest.getBuildId())) {
        // BeanUtil.copyProperties(buildRequest, build);
        // }
        // });
        // return build;
        // }).collect(Collectors.toList());
        // houses = houses.stream().map(house -> {
        // housesRequest.forEach(houseRequest -> {
        // if (house.getHouseId().equals(houseRequest.getHouseId())) {
        // BeanUtil.copyProperties(houseRequest, house);
        // }
        // });
        // return house;
        // }).collect(Collectors.toList());
        // 判断是否保存缴存明细
        BigDecimal totalDepositMoney = new BigDecimal("0.00");
        if (ObjectUtil.isNotEmpty(details)) {
            // 防止用户重复提交缴存明细信息
            List<Long> detailIds = gwjDepositBusinessDetailService.selectGwjDepositBusinessDetailByBizId(business.getBizId()).stream().map(GwjDepositBusinessDetail::getDepositDetailId).collect(Collectors.toList());
            // 判断操作之前是否存在缴存明细信息
            if (ObjectUtil.isNotEmpty(detailIds)) {
                // 批量删除缴存明细信息
                gwjDepositBusinessDetailService.deleteGwjDepositBusinessDetailByDepositDetailIds(detailIds.stream().toArray(Long[]::new));
            }
            details = details.stream().map(detail -> {
                detail.setBizId(business.getBizId());
                detail.setMoneyId(money.getBizMoneyId());
                return detail;
            }).collect(Collectors.toList());
            // 保存业务缴费明细
            gwjDepositBusinessDetailService.insertBatchGwjDepositBusinessDetail(details);
            money.setDepositTime(new DateTime());
        }
        // BigDecimal houseTotalMoney = new BigDecimal("0.00");
        // for (GwjDepositBusinessHouse house : houses) {
        // houseTotalMoney = houseTotalMoney.add(house.getDepositMoney());
        // }
        // money.setTotalMoney(houseTotalMoney);
        // business.setLackMoney(houseTotalMoney);
        if (ObjectUtil.isNotEmpty(details)) {
            for (GwjDepositBusinessDetail item : details) {
                totalDepositMoney = totalDepositMoney.add(item.getDepositMoney());
            }
            money.setFactMoney(totalDepositMoney);
            business.setLackMoney(money.getTotalMoney().subtract(money.getFactMoney()));
        }
        // // 重置小区、楼栋、房屋信息
        // if
        // (ObjectUtil.isNotEmpty(gwjDepositBusinessCommunityService.selectGwjDepositBusinessCommunityByBizId(business.getBizId())))
        // {
        // gwjDepositBusinessCommunityService.deleteGwjDepositBusinessCommunityByBizId(business.getBizId());
        // }
        // if
        // (ObjectUtil.isNotEmpty(gwjDepositBusinessBuildService.deleteGwjDepositBusinessBuildByBizId(business.getBizId())))
        // {
        // gwjDepositBusinessBuildService.deleteGwjDepositBusinessBuildByBizId(business.getBizId());
        // }
        // if
        // (ObjectUtil.isNotEmpty(gwjDepositBusinessHouseService.deleteGwjDepositBusinessHouseByBizId(business.getBizId())))
        // {
        // gwjDepositBusinessHouseService.deleteGwjDepositBusinessHouseByBizId(business.getBizId());
        // }
        // 判断当前用户是否有权限
        gwjDepositBusinessService.checkPermission(business.getBizId());
        business.setStatus(GwjBusinessStatus.NORMAL.getCode());
        // 更新业务信息
        gwjDepositBusinessService.updateGwjDepositBusiness(business);
        // // 新增小区信息
        // gwjDepositBusinessCommunityService.insertGwjDepositBusinessCommunity(community);
        // 处理业务数据
        // community.setBizId(business.getBizId());
        // builds = builds.stream().map(build -> {
        // build.setBizId(community.getBizId());
        // build.setAreaId(community.getAreaId());
        // build.setStreetId(community.getStreetId());
        // build.setBizCommunityId(community.getBizCommunityId());
        // return build;
        // }).collect(Collectors.toList());
        // houses = houses.stream().map(house -> {
        // house.setBizId(business.getBizId());
        // return house;
        // }).collect(Collectors.toList());
        // // 新增楼栋信息
        // gwjDepositBusinessBuildService.insertGwjDepositBusinessBuilds(builds);
        // // 新增房屋信息
        // gwjDepositBusinessHouseService.insertGwjDepositBusinessHouses(houses);
        // 更新业务金额信息
        gwjDepositBusinessMoneyService.updateGwjDepositBusinessMoney(money);
        // 更新日志信息
        gwjDepositBusinessLogService.addSaveLog(business);

    }

    /**
     * 业主缴存（提交）
     *
     * @param saveSubmitRequest 提交参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitBusiness(GwjOwnerDepositSaveSubmitRequest saveSubmitRequest) {
        GwjDepositBusinessRequest businessRequest = saveSubmitRequest.getBusiness();
        GwjDepositBusinessLog businessLog = saveSubmitRequest.getBusinessLog();
        // 校验当前用户是否拥有权限
        if (ObjectUtil.isNull(businessRequest.getBizId())) {
            throw new ServiceException("请先保存业务");
        }
        GwjDepositBusiness business = gwjDepositBusinessService.checkPermission(businessRequest.getBizId());
        // 判断是否已有数据 可以直接提交
        if (ObjectUtil.isNull(gwjDepositBusinessCommunityService.selectGwjDepositBusinessCommunityListByBizId(business.getBizId()))) {
            throw new ServiceException("小区信息不能为空");
        }
        if (ObjectUtil.isEmpty(gwjDepositBusinessBuildService.selectGwjDepositBusinessBuildListByBizId(business.getBizId()))) {
            throw new ServiceException("小区楼栋不能为空");
        }
        if (ObjectUtil.isEmpty(gwjDepositBusinessHouseService.selectGwjDepositBusinessHouseListByBizIdAndSelectFlag(business.getBizId(), ""))) {
            throw new ServiceException("小区房屋不能为空");
        }

        // 设置日志流程步骤,业务编号
        businessLog.setStepId(business.getStepId());
        businessLog.setBizId(business.getBizId());
        // 获取当前步骤所有配置
        List<String> configInfos = remoteGwjConfigService.selectGwjConfStepConfigInfoCodeListByBizFlagId(businessLog.getStepId(), SecurityConstants.INNER).getData();
        // 判断是否需要选收件人
        if (StringUtils.containsAny(configInfos, "submit_choose_user") && StringUtils.isAnyBlank(com.yida.common.core.text.Convert.toStr(businessLog.getUserId()), businessLog.getRealName())) {
            throw new ServiceException("请选择下一位收件人");
        }
        // 当前办理人员
        business.setCurrentOperId(StringUtils.isNull(businessLog.getUserId()) ? SecurityUtils.getUserId() : businessLog.getUserId());
        business.setCurrentOperName(StringUtils.isBlank(businessLog.getRealName()) ? SecurityUtils.getLoginUser().getSysUser().getRealName() : businessLog.getRealName());
        // 当前步骤
        GwjConfStep nextStep = remoteGwjConfigService.selectNextGwjConfStepByFlagId(GwjBusinessFlagType.OWNER_DEPOSIT.getCode(), business.getStepId(), SecurityConstants.INNER).getData();
        if (ObjectUtil.isEmpty(nextStep)) {
            throw new ServiceException("当前案例已完结");
        }
        // 校验参数
        if (nextStep.getStepId().equals(GwjOwnerDepositStepEnum.FINANCIAL_RECONCILIATION.getStepId())) {
            // 缴费详情
            if (ObjectUtil.isEmpty(gwjDepositBusinessDetailService.selectGwjDepositBusinessDetailByBizId(business.getBizId()))) {
                throw new ServiceException("缴费详情不能为空");
            }

            // 总账流水
            GwjFinanceAccountMain accountMain = new GwjFinanceAccountMain();
            accountMain.setBizId(business.getBizId());
            if (ObjectUtil.isEmpty(remoteGwjFinanceService.selectGwjFinanceAccountMainList(accountMain, SecurityConstants.INNER).getData())) {
                throw new ServiceException("未生成流水，请先生成流水");
            }
            // 分类账流水
            if (ObjectUtil.isEmpty(remoteGwjFinanceService.selectGwjFinanceAccountDetailListByBizId(business.getBizId(), SecurityConstants.INNER).getData())) {
                throw new ServiceException("未生成流水，请先生成流水");
            }
        }
        if (nextStep.getStepId().equals(GwjOwnerDepositStepEnum.CASE_REVIEW.getStepId())) {
            GwjDepositBusinessMoney money = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(business.getBizId());
            if (!money.getStatus().equals(GwjCheckAccountStatus.CHECK_SUCCESS.getCode())) {
                throw new ServiceException("财务还未对账，清先进行财务对账");
            }
        }

        business.setStepId(nextStep.getStepId());
        // 更新业务
        business.setStatus(GwjBusinessStatus.NORMAL.getCode());
        gwjDepositBusinessService.updateGwjDepositBusiness(business);
        // 日志
        gwjDepositBusinessLogService.addSubmitLog(businessLog, business);
        // 判断是不是已经最后一步
        if (ObjectUtil.isNull(remoteGwjConfigService.selectNextGwjConfStepByFlagId(GwjBusinessFlagType.OWNER_DEPOSIT.getCode(), nextStep.getStepId(), SecurityConstants.INNER).getData())) {
            // 将业务数据更新基础数据
            // 已选择房屋数据
            List<GwjDepositBusinessHouse> gwjDepositBusinessHouses = gwjDepositBusinessHouseService.selectGwjDepositBusinessHouseListByBizIdAndSelectFlag(business.getBizId(), GwjBusinessHouseStatus.CONFIRM.getCode());
            if (ObjectUtil.isNotEmpty(gwjDepositBusinessHouses)) {
                gwjDepositBusinessHouses.forEach(gwjDepositBusinessHouse -> {
                    // 公用户做插入
                    if (GwjConstants.YES.equals(gwjDepositBusinessHouse.getPublicFlag())) {
                        // 判断所属楼栋是不是也是公用户
                        GwjDepositBusinessBuild gwjDepositBusinessBuild = gwjDepositBusinessBuildService.selectGwjDepositBusinessBuildListByBizIdAndBuildCode(business.getBizId(), gwjDepositBusinessHouse.getBuildCode());
                        if (ObjectUtil.isNotNull(gwjDepositBusinessBuild) && GwjConstants.YES.equals(gwjDepositBusinessBuild.getPublicFlag())) {
                            // 判断基础库是否已存在
                            if (ObjectUtil.isNull(buildService.selectGwjBaseBuildByAreaIdAndBuildCode(business.getAreaId(), gwjDepositBusinessHouse.getBuildCode()))) {
                                // 插入
                                GwjBaseBuild baseBuild = GwjConvertUtil.convert(gwjDepositBusinessBuild, GwjBaseBuild.class);
                                buildService.insertGwjBaseBuild(baseBuild);
                            }
                        }
                        GwjBaseHouse gwjBaseHouse = houseService.selectGwjBaseHouseByGwjBaseHouseId(gwjDepositBusinessHouse.getHouseId());
                        if (ObjectUtil.isEmpty(gwjBaseHouse)) {
                            GwjBaseHouse baseHouse = GwjConvertUtil.convert(gwjDepositBusinessHouse, GwjBaseHouse.class);
                            BigDecimal depositMoney = gwjDepositBusinessHouse.getDepositMoney();
                            baseHouse.setDepositMoney(depositMoney);
                            // if (ObjectUtil.isEmpty(baseHouse.getUsedMoney()) ||
                            // baseHouse.getUsedMoney().equals(BigDecimal.ZERO)) {
                            // String configValue =
                            // com.yida.common.core.text.Convert.toStr(redisService.getCacheObject(GwjCacheConstants.GWJ_CONFIG_KEY
                            // + GwjCacheConstants.BLOCKED_MONEY_KEY));
                            // baseHouse.setBlockedMoney(depositMoney.multiply(new BigDecimal(configValue)));
                            // }
                            baseHouse.setCurrentMoney(depositMoney);
                            if (1 != houseService.insertGwjBaseHouse(baseHouse)) {
                                throw new ServiceException("基础房屋数据新增失败");
                            }
                        } else {
                            BigDecimal depositMoney = gwjDepositBusinessHouse.getDepositMoney();
                            gwjBaseHouse.setDepositMoney(gwjBaseHouse.getDepositMoney().add(depositMoney));
                            // if (ObjectUtil.isEmpty(baseHouse.getUsedMoney()) ||
                            // baseHouse.getUsedMoney().equals(BigDecimal.ZERO)) {
                            // String configValue =
                            // com.yida.common.core.text.Convert.toStr(redisService.getCacheObject(GwjCacheConstants.GWJ_CONFIG_KEY
                            // + GwjCacheConstants.BLOCKED_MONEY_KEY));
                            // baseHouse.setBlockedMoney(depositMoney.multiply(new BigDecimal(configValue)));
                            // }
                            gwjBaseHouse.setCurrentMoney(null != gwjBaseHouse.getCurrentMoney() ? gwjBaseHouse.getCurrentMoney().add(depositMoney) : depositMoney);
                            if (1 != houseService.updateGwjBaseHouse(gwjBaseHouse)) {
                                throw new ServiceException("基础房屋数据修改失败");
                            }
                        }
                    } else {
                        GwjBaseHouse baseHouse = houseService.selectGwjBaseHouseByGwjBaseHouseId(gwjDepositBusinessHouse.getHouseId());
                        if (ObjectUtil.isNotNull(baseHouse)) {
                            BigDecimal depositMoney = null != gwjDepositBusinessHouse.getDepositMoney() ? gwjDepositBusinessHouse.getDepositMoney() : new BigDecimal("0.00");
                            baseHouse.setDepositMoney(depositMoney.add(baseHouse.getDepositMoney() == null ? new BigDecimal("0.00") : baseHouse.getDepositMoney()));
                            baseHouse.setCurrentMoney(null != baseHouse.getCurrentMoney() ? baseHouse.getCurrentMoney().add(depositMoney) : depositMoney);
                            // if (ObjectUtil.isEmpty(baseHouse.getUsedMoney()) ||
                            // baseHouse.getUsedMoney().equals(BigDecimal.ZERO)) {
                            if (StringUtils.isNull(baseHouse.getUsedMoney()) || baseHouse.getUsedMoney().compareTo(BigDecimal.ZERO) == 0) {// 20230726
                                String configValue = com.yida.common.core.text.Convert.toStr(redisService.getCacheObject(GwjCacheConstants.GWJ_CONFIG_KEY + GwjCacheConstants.BLOCKED_MONEY_KEY));
                                baseHouse.setBlockedMoney(depositMoney.multiply(new BigDecimal(configValue)));
                            }
                            houseService.updateGwjBaseHouse(baseHouse);
                        }
                    }
                });
            }
            // 业务更新为完结
            business.setStatus(GwjBusinessStatus.FINISH.getCode());
            gwjDepositBusinessService.updateGwjDepositBusiness(business);
            gwjDepositBusinessLogService.addFinishLog(business);
        }
    }

    /**
     * 业主缴存（删页）
     *
     * @param bizId 业务主键
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBusinessData(Long bizId) {
        // 验证是否有操作权限
        GwjDepositBusiness business = gwjDepositBusinessService.checkPermission(bizId);
        if (ObjectUtil.isEmpty(business)) {
            throw new ServiceException("未查询到对应业务信息");
        }
        if (!GwjOwnerDepositStepEnum.ACCEPTED.getStepId().equals(business.getStepId())) {
            throw new ServiceException("当前步骤不能删页");
        }
        // 清空业务信息
        business.setApplicantName("");
        business.setPhone("");
        business.setBizName("");
        business.setArchivesNum("");
        business.setLackMoney(new BigDecimal("0.00"));
        business.setCommentFlag(GwjConstants.NO);
        business.setStatus(GwjBusinessStatus.NORMAL.getCode());
        business.setApplicantTime(DateUtils.getNowDate());
        business.setPropertyAddress("");
        gwjDepositBusinessService.updateGwjDepositBusiness(business);
        // 删除业务小区、房屋、楼栋、业主
        gwjDepositBusinessService.deleteCommunityBuildHouseOwnerData(bizId);
        // 删除缴存明细信息
        gwjDepositBusinessDetailService.deleteBatchGwjDepositBusinessDetailByBizId(bizId);
        GwjDepositBusinessMoney gwjDepositBusinessMoney = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(bizId);

        // 清空业务金额
        BigDecimal zero = new BigDecimal("0.00");
        gwjDepositBusinessMoney.setTotalMoney(zero);
        gwjDepositBusinessMoney.setTotalSize(zero);
        gwjDepositBusinessMoney.setFactMoney(zero);
        gwjDepositBusinessMoney.setFactSize(zero);
        gwjDepositBusinessMoney.setTotalOwner(0L);
        gwjDepositBusinessMoney.setDepositTime(null);
        gwjDepositBusinessMoney.setCalculateTypeId(20L);
        gwjDepositBusinessMoneyService.updateGwjDepositBusinessMoney(gwjDepositBusinessMoney);
        // 删除总账流水
        if (StringUtils.isNotNull(remoteGwjFinanceService.selectGwjFinanceAccountMainByBizId(bizId, SecurityConstants.INNER).getData())) {
            remoteGwjFinanceService.deleteGwjFinanceAccountMainByBizId(bizId, SecurityConstants.INNER);
        }
        // 删除分类账流水
        if (StringUtils.isNotEmpty(remoteGwjFinanceService.selectGwjFinanceAccountDetailListByBizId(bizId, SecurityConstants.INNER).getData())) {
            remoteGwjFinanceService.deleteGwjFinanceAccountDetailByBizId(bizId, SecurityConstants.INNER);
        }
        // 清空日志
        gwjDepositBusinessLogService.deleteGwjDepositBusinessLogByBizId(bizId);
    }

    /**
     * 业主缴存（删除案例）
     *
     * @param bizId 业务主键
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeBusiness(Long bizId) {
        // 校验权限
        GwjDepositBusiness business = gwjDepositBusinessService.checkPermission(bizId);
        // 检验业务
        if (ObjectUtil.isEmpty(business)) {
            throw new ServiceException("未查询到对应业务");
        }
        business.setStatus(GwjBusinessStatus.FINISH.getCode());
        // 更新业务信息
        if (1 != gwjDepositBusinessService.updateGwjDepositBusiness(business)) {
            throw new ServiceException("案例删除失败");
        }

    }

    /**
     * 分页查询
     *
     * @param gwjDepositBusiness 查询参数
     * @return
     */
    @Override
    public List<GwjDepositPageDTO> getPage(GwjDepositBusiness gwjDepositBusiness) {
        // 设置业务标识
        gwjDepositBusiness.setBizFlagId(GwjBusinessFlagType.OWNER_DEPOSIT.getCode());
        // 设置查询参数
        PageUtils.startPage();

        // 查询分页对象
        return gwjDepositBusinessMapper.selectGwjDepositBusinessPage(gwjDepositBusiness);
    }

    /**
     * 查询详情
     *
     * @param bizId 业务主键
     * @return 业主缴存详情对象
     */
    @Override
    public GwjOwnerDepositDetailDTO getDetail(Long bizId) {
        // 查询业务、小区、楼栋、房屋、金额、缴存明细信息
        GwjDepositBusiness business = gwjDepositBusinessService.selectGwjDepositBusinessByBizId(bizId);
        GwjDepositBusinessCommunity community = gwjDepositBusinessCommunityService.selectGwjDepositBusinessCommunityByBizId(bizId);
        List<GwjDepositBusinessBuild> builds = gwjDepositBusinessBuildService.selectGwjDepositBusinessBuildListByBizId(bizId);
        List<GwjDepositBusinessHouse> houses = gwjDepositBusinessHouseService.selectGwjDepositBusinessHouseListByBizId(bizId);
        GwjDepositBusinessMoney money = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(bizId);
        List<GwjDepositBusinessDetail> payments = gwjDepositBusinessDetailService.selectGwjDepositBusinessDetailByBizId(bizId);
        String[] houseCode = null;
        Map<String, String> ownerMap = new HashMap<String, String>();
        if (ObjectUtil.isNotEmpty(houses)) {
            houseCode = houses.stream().map(GwjDepositBusinessHouse::getHouseCode).toArray(String[]::new);
        }
        if (ObjectUtil.isNotEmpty(houseCode)) {
            for (GwjBaseOwner owner : baseOwnerService.selectGwjBaseListByGwjBaseHouseCodes(houseCode, money.getAreaId())) {
                if (ObjectUtil.isEmpty(ownerMap.get(owner.getHouseCode()))) {
                    ownerMap.put(owner.getHouseCode(), owner.getOwnerName());
                } else {
                    List<String> ownerNameList = new ArrayList<String>(2);
                    ownerNameList.add(ownerMap.get(owner.getHouseCode()));
                    ownerNameList.add(owner.getOwnerName());
                    ownerMap.put(owner.getHouseCode(), StringUtils.join(ownerNameList, ","));
                }
            }

        }
        // 处理业务数据
        GwjOwnerDepositDetailDTO detail = new GwjOwnerDepositDetailDTO();
        GwjDepositBusinessDetailDTO businessDetail = new GwjDepositBusinessDetailDTO();
        GwjDepositCommunityDetailDTO communityDetail = new GwjDepositCommunityDetailDTO();
        GwjDepositMoneyDetailDTO moneyDetail = new GwjDepositMoneyDetailDTO();
        List<GwjDepositPaymentDetailDTO> paymentDetailList = new ArrayList<GwjDepositPaymentDetailDTO>();
        // 拷贝业务信息
        BeanUtil.copyProperties(business, businessDetail);
        BeanUtil.copyProperties(community, communityDetail);
        List<GwjOwnerDepositBuildDetailDTO> buildDetailList = builds.stream().map(build -> {
            GwjOwnerDepositBuildDetailDTO buildDetailDTO = new GwjOwnerDepositBuildDetailDTO();
            BeanUtil.copyProperties(build, buildDetailDTO);
            return buildDetailDTO;
        }).collect(Collectors.toList());
        List<GwjOwnerDepositHouseDetailDTO> houseDetailList = new ArrayList<GwjOwnerDepositHouseDetailDTO>();
        for (GwjDepositBusinessHouse house : houses) {
            GwjOwnerDepositHouseDetailDTO houseDetailDTO = new GwjOwnerDepositHouseDetailDTO();
            BeanUtil.copyProperties(house, houseDetailDTO);
            houseDetailDTO.setOwnerName(ownerMap.get(house.getHouseCode()));
            houseDetailList.add(houseDetailDTO);
        }
        BeanUtil.copyProperties(money, moneyDetail);
        if (ObjectUtil.isNotEmpty(payments)) {
            paymentDetailList = payments.stream().map(payment -> {
                GwjDepositPaymentDetailDTO paymentDetailDTO = new GwjDepositPaymentDetailDTO();
                BeanUtil.copyProperties(payment, paymentDetailDTO);
                return paymentDetailDTO;
            }).collect(Collectors.toList());
        }
        // 参数赋值
        detail.setBusinessDetail(businessDetail);
        detail.setCommunityDetail(communityDetail);
        detail.setBuildDetails(buildDetailList);
        detail.setHouseDetails(houseDetailList);
        detail.setMoneyDetail(moneyDetail);
        detail.setPaymentDetails(paymentDetailList);
        return detail;
    }

    /**
     * 主页提交
     *
     * @param businessLog
     */
    @Override
    public void indexSubmit(GwjDepositBusinessLog businessLog) {
        Long bizId = businessLog.getBizId();
        // 校验权限
        GwjDepositBusiness business = gwjDepositBusinessService.checkPermission(bizId);
        if (ObjectUtil.isEmpty(business)) {
            throw new ServiceException("未查询到对应业务信息");
        }
        // 查询小区、楼栋、房屋、金额、缴费详情信息
        GwjDepositBusinessCommunity community = gwjDepositBusinessCommunityService.selectGwjDepositBusinessCommunityByBizId(bizId);
        List<GwjDepositBusinessBuild> builds = gwjDepositBusinessBuildService.selectGwjDepositBusinessBuildListByBizId(bizId);
        List<GwjDepositBusinessHouse> houseList = gwjDepositBusinessHouseService.selectGwjDepositBusinessHouseListByBizId(bizId);
        GwjDepositBusinessMoney money = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(bizId);
        List<GwjDepositBusinessDetail> details = gwjDepositBusinessDetailService.selectGwjDepositBusinessDetailByBizId(bizId);
        // 处理业务参数
        GwjOwnerDepositSaveSubmitRequest saveSubmitRequest = new GwjOwnerDepositSaveSubmitRequest();
        GwjDepositBusinessRequest businessRequest = new GwjDepositBusinessRequest();
        GwjDepositBusinessCommunityRequest communityRequest = new GwjDepositBusinessCommunityRequest();
        List<GwjDepositBusinessBuildRequest> buildRequests = new ArrayList<GwjDepositBusinessBuildRequest>();
        List<GwjDepositBusinessHouseRequest> houseRequests = new ArrayList<GwjDepositBusinessHouseRequest>();
        GwjDepositBusinessMoneyRequest moneyRequest = new GwjDepositBusinessMoneyRequest();
        List<GwjDepositBusinessDetailRequest> detailRequests = new ArrayList<GwjDepositBusinessDetailRequest>();
        GwjDepositBusinessLogRequest logRequest = new GwjDepositBusinessLogRequest();
        // 拷贝参数
        BeanUtil.copyProperties(business, businessRequest);
        BeanUtil.copyProperties(community, communityRequest);
        buildRequests = builds.stream().map(item -> {
            GwjDepositBusinessBuildRequest buildRequest = new GwjDepositBusinessBuildRequest();
            BeanUtil.copyProperties(item, buildRequest);
            return buildRequest;
        }).collect(Collectors.toList());
        houseRequests = houseList.stream().map(item -> {
            GwjDepositBusinessHouseRequest houseRequest = new GwjDepositBusinessHouseRequest();
            BeanUtil.copyProperties(item, houseRequest);
            return houseRequest;
        }).collect(Collectors.toList());
        detailRequests = details.stream().map(item -> {
            GwjDepositBusinessDetailRequest detailRequest = new GwjDepositBusinessDetailRequest();
            BeanUtil.copyProperties(item, detailRequest);
            return detailRequest;
        }).collect(Collectors.toList());
        BeanUtil.copyProperties(businessLog, logRequest);
        // 参数赋值
        saveSubmitRequest.setBusiness(businessRequest);
        saveSubmitRequest.setBusinessCommunity(communityRequest);
        saveSubmitRequest.setBusinessBuilds(buildRequests);
        saveSubmitRequest.setBusinessHouses(houseRequests);
        saveSubmitRequest.setBusinessMoney(moneyRequest);
        saveSubmitRequest.setDetails(detailRequests);
        saveSubmitRequest.setBusinessLog(logRequest);
        submitBusiness(saveSubmitRequest);
    }

    /**
     * 选择小区
     *
     * @param editCommunityRequest 小区业务参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void chooseCommunity(GwjOwnerDepositChooseCommunityRequest editCommunityRequest) {
        // 校验权限
        GwjDepositBusiness business = gwjDepositBusinessService.checkPermission(editCommunityRequest.getBizId());
        // 校验步骤
        if (!GwjOwnerDepositStepEnum.ACCEPTED.getStepId().equals(business.getStepId())) {
            throw new ServiceException("当前业务步骤不允许修改小区信息");
        }
        // 重置业务小区、楼栋、房屋信息
        gwjDepositBusinessCommunityService.deleteGwjDepositBusinessCommunityByBizId(business.getBizId());
        gwjDepositBusinessBuildService.deleteGwjDepositBusinessBuildByBizId(business.getBizId());
        gwjDepositBusinessHouseService.deleteGwjDepositBusinessHouseByBizId(business.getBizId());
        // 查询业务金额信息
        GwjDepositBusinessMoney money = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(business.getBizId());
        if (ObjectUtil.isEmpty(money)) {
            throw new ServiceException("业务金额信息查询失败");
        }
        // 设置业务常量
        BigDecimal zero = new BigDecimal("0.00");
        // 重置业务金额参数
        money.setTotalMoney(zero);
        money.setFactMoney(zero);
        money.setFactSize(zero);
        money.setTotalSize(zero);
        money.setDepositObject("业主个人缴存");
        money.setCapitalProperty("普通商住房");
        money.setStatus("0");
        money.setDepositTime(null);
        money.setCalculateTypeId(20L);
        money.setAccountTime(null);
        money.setTotalOwner(0L);
        // 更新业务金额参数
        if (1 != gwjDepositBusinessMoneyService.updateGwjDepositBusinessMoney(money)) {
            throw new ServiceException("业务金额重置失败");
        }
        // 重置业务参数
        business.setLackMoney(new BigDecimal("0.00"));
        business.setCommentFlag(GwjConstants.NO);
        business.setStatus(GwjBusinessStatus.NORMAL.getCode());
        // 更新业务参数
        if (1 != gwjDepositBusinessMapper.updateGwjDepositBusiness(business)) {
            throw new ServiceException("业务数据重置失败");
        }
        // 拉取社区基础数据
        GwjBaseCommunity gwjBaseCommunity = communityService.selectGwjBaseCommunityByGwjBaseCommunityId(editCommunityRequest.getCommunityRequest().getCommunityId());
        // 处理业务小区信息
        GwjDepositBusinessCommunity community = new GwjDepositBusinessCommunity();
        BeanUtil.copyProperties(gwjBaseCommunity, community);
        BeanUtil.copyProperties(editCommunityRequest.getCommunityRequest(), community);
        community.setBizId(business.getBizId());
        // 新增业务小区数据
        if (1 != gwjDepositBusinessCommunityService.insertGwjDepositBusinessCommunity(community)) {
            throw new ServiceException("业务小区修改失败");
        }

    }

    /**
     * 修改业务楼栋参数
     *
     * @param editBuildRequest 楼栋业务参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editBuild(GwjOwnerDepositBusinessBuildRequest editBuildRequest) {
        // 校验权限
        GwjDepositBusiness business = gwjDepositBusinessService.checkPermission(editBuildRequest.getBizId());
        // 校验步骤
        if (!GwjOwnerDepositStepEnum.ACCEPTED.getStepId().equals(business.getStepId())) {
            throw new ServiceException("当前业务步骤不允许修改业务楼栋信息");
        }
        // 处理参数
        GwjDepositBusinessBuildRequest buildRequest = editBuildRequest.getBuildRequest();
        // 校验参数
        if (ObjectUtil.isEmpty(buildRequest.getBizBuildId())) {
            throw new ServiceException("业务楼栋主键不能为空");
        }
        // 查询业务楼栋信息
        GwjDepositBusinessBuild build = gwjDepositBusinessBuildService.selectGwjDepositBusinessBuildByBizBuildId(buildRequest.getBizBuildId());
        if (ObjectUtil.isEmpty(build)) {
            throw new ServiceException("未查询到对应业务楼栋信息");
        }
        // 处理业务楼栋信息
        build.setBuildName(buildRequest.getBuildName());
        build.setBuildType(buildRequest.getBuildType());
        // 更新业务楼栋信息
        if (1 != gwjDepositBusinessBuildService.updateGwjDepositBusinessBuild(build)) {
            throw new ServiceException("业务楼栋信息修改失败");
        }
    }

    /**
     * 修改业务房屋信息
     *
     * @param editHouseRequest 房屋业务信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editHouse(GwjOwnerDepositBusinessHouseRequest editHouseRequest) {
        // 校验权限
        GwjDepositBusiness business = gwjDepositBusinessService.checkPermission(editHouseRequest.getBizId());
        // 校验步骤
        if (!GwjOwnerDepositStepEnum.ACCEPTED.getStepId().equals(business.getStepId())) {
            throw new ServiceException("当前业务步骤不允许修改房屋信息");
        }
        // 查询业务房屋信息
        GwjDepositBusinessHouse house = gwjDepositBusinessHouseService.selectGwjDepositBusinessHouseByBizHouseId(editHouseRequest.getBizHouseId());
        if (ObjectUtil.isEmpty(house)) {
            throw new ServiceException("未查询到对应业务房屋信息");
        }
        // 查询业务金额信息
        GwjDepositBusinessMoney money = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(editHouseRequest.getBizId());
        if (ObjectUtil.isEmpty(money)) {
            throw new ServiceException("未查询到对应业务金额信息");
        }
        // 业务金额信息应缴金额扣除原本房屋缴款金额
        money.setTotalMoney(null != money.getTotalMoney() && !new BigDecimal("0.00").equals(money.getTotalMoney()) ? money.getTotalMoney().subtract(house.getDepositMoney()) : house.getDepositMoney());
        // 业务信息应补缴金额扣除原本房屋缴款
        business.setLackMoney(null != business.getLackMoney() && !new BigDecimal("0.00").equals(business.getLackMoney()) ? business.getLackMoney().subtract(house.getDepositMoney()) : house.getDepositMoney());
        // 处理业务房屋信息
        house.setCalculateTypeId(editHouseRequest.getCalculateTypeId());
        house.setDepositMoney(editHouseRequest.getDepositMoney());
        // 更新业务房屋信息
        if (1 != gwjDepositBusinessHouseService.updateGwjDepositBusinessHouse(house)) {
            throw new ServiceException("业务房屋信息修改失败");
        }
        // 业务金额信息应缴金额增加修改后房屋缴款金额
        money.setTotalMoney(money.getTotalMoney().add(house.getDepositMoney()));
        // 业务信息应补缴金额增加修改后房屋缴款
        business.setLackMoney(business.getLackMoney().add(house.getDepositMoney()));
        // 更新业务金额信息
        if (1 != gwjDepositBusinessMoneyService.updateGwjDepositBusinessMoney(money)) {
            throw new ServiceException("业务金额信息更新失败");
        }
        // 更新业务信息
        if (1 != gwjDepositBusinessMapper.updateGwjDepositBusiness(business)) {
            throw new ServiceException("业务信息更新失败");
        }
    }

    /**
     * 选择楼栋
     *
     * @param chooseBuildRequest 选择参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void chooseBuilds(GwjOwnerDepositChooseBuildRequest chooseBuildRequest) {
        // 校验权限
        GwjDepositBusiness business = gwjDepositBusinessService.checkPermission(chooseBuildRequest.getBizId());
        // 校验步骤
        if (!GwjOwnerDepositStepEnum.ACCEPTED.getStepId().equals(business.getStepId())) {
            throw new ServiceException("当前业务步骤不允许修改房屋信息");
        }
        // 拉取基础楼栋信息
        List<GwjBaseBuild> baseBuilds = buildService.selectGwjBaseBuildByBuildCodes(chooseBuildRequest.getBuildCodes());
        // 判断楼栋数据是否对等
        if (chooseBuildRequest.getBuildCodes().length != baseBuilds.size()) {
            throw new ServiceException("房屋编号有误，请重新选择");
        }
        // 查询业务小区
        GwjDepositBusinessCommunity community = gwjDepositBusinessCommunityService.selectGwjDepositBusinessCommunityByBizId(chooseBuildRequest.getBizId());
        if (ObjectUtil.isEmpty(community)) {
            throw new ServiceException("未查询到对应业务小区");
        }
        // 处理业务楼栋信息
        List<GwjDepositBusinessBuild> buildList = baseBuilds.stream().map(item -> {
            GwjDepositBusinessBuild build = new GwjDepositBusinessBuild();
            BeanUtil.copyProperties(item, build);
            build.setBizId(business.getBizId());
            build.setBizCommunityId(community.getBizCommunityId());
            return build;
        }).collect(Collectors.toList());
        // 新增楼栋信息
        gwjDepositBusinessBuildService.insertGwjDepositBusinessBuilds(buildList);
    }

    /**
     * 删除楼栋
     *
     * @param bizId 业务主键
     * @param bizBuildIds 业务楼栋主键
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBuildData(Long bizId, Long[] bizBuildIds) {
        // 校验权限
        GwjDepositBusiness business = gwjDepositBusinessService.checkPermission(bizId);
        // 校验步骤
        if (!GwjOwnerDepositStepEnum.ACCEPTED.getStepId().equals(business.getStepId())) {
            throw new ServiceException("当前业务步骤不允许修改房屋信息");
        }
        // 判断业务楼栋主键是否为空
        if (0 == bizBuildIds.length) {
            throw new ServiceException("请选中楼栋");
        }
        // 查询业务楼栋信息
        List<GwjDepositBusinessBuild> buildList = gwjDepositBusinessBuildService.selectGwjDepositBusinessBuildListByBizBuildId(bizBuildIds);
        if (ObjectUtil.isEmpty(buildList)) {
            throw new ServiceException("未查询到对应业务楼栋信息");
        }
        // 处理业务楼栋编号
        String[] buildCodes = buildList.stream().map(GwjBaseBusinessBuild::getBuildCode).toArray(String[]::new);
        // 判断楼栋条数是相等
        if (bizBuildIds.length != buildList.size()) {
            throw new ServiceException("楼栋主键有误，请重新选择");
        }
        // 查询业务房屋信息
        List<GwjDepositBusinessHouse> houseList = gwjDepositBusinessHouseService.selectGwjDepositBusinessHouseListByBizIdAndBuildCodes(bizId, buildCodes);
        // 查询业务房屋主键
        Long[] bizHouseIds = houseList.stream().map(GwjDepositBusinessHouse::getBizHouseId).toArray(Long[]::new);
        // 计算取消房屋缴存金额
        BigDecimal totalDepositMoney = new BigDecimal(houseList.stream().map(GwjDepositBusinessHouse::getDepositMoney).mapToDouble(BigDecimal::doubleValue).sum());
        // 查询业务金额信息
        GwjDepositBusinessMoney money = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(bizId);
        if (ObjectUtil.isEmpty(money)) {
            throw new ServiceException("未查询到对应业务金额信息");
        }
        // 业务金额信息应缴金额扣除取消房屋缴存金额
        money.setTotalMoney(null != money.getTotalMoney() && !new BigDecimal("0.00").equals(money.getTotalMoney()) ? money.getTotalMoney().subtract(totalDepositMoney) : new BigDecimal("0.00"));
        money.setTotalOwner(null != money.getTotalOwner() && 0L != money.getTotalOwner() ? money.getTotalOwner() - Long.valueOf(houseList.size()) : 0L);
        money.setCalculateTypeId(null);
        // 更新业务金额信息
        if (1 != gwjDepositBusinessMoneyService.updateGwjDepositBusinessMoney(money)) {
            throw new ServiceException("业务金额更新失败");
        }
        // 业务信息应补缴金额扣除取消房屋缴存金额
        business.setLackMoney(null != business.getLackMoney() && !new BigDecimal("0.00").equals(business.getLackMoney()) ? money.getTotalMoney().subtract(totalDepositMoney) : new BigDecimal("0.00"));
        // 更新业务信息
        if (1 != gwjDepositBusinessMapper.updateGwjDepositBusiness(business)) {
            throw new ServiceException("业务信息更新失败");
        }
        // 删除业务房屋信息
        if (0 != bizHouseIds.length) {
            if (0 == gwjDepositBusinessHouseService.deleteGwjDepositBusinessHouseByBizHouseIds(bizHouseIds)) {
                throw new ServiceException("业务房屋信息删除失败");
            }
        }
        // 删除业务楼栋信息
        if (0 == gwjDepositBusinessBuildService.deleteGwjDepositBusinessBuildByBizBuildIds(bizBuildIds)) {
            throw new ServiceException("业务楼栋信息删除失败");
        }
    }

    /**
     * 选择房屋
     *
     * @param chooseHouseRequest 选择参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void chooseHouses(GwjOwnerDepositChooseHouseRequest chooseHouseRequest) {
        // 校验权限
        GwjDepositBusiness business = gwjDepositBusinessService.checkPermission(chooseHouseRequest.getBizId());
        // 校验步骤
        if (!GwjOwnerDepositStepEnum.ACCEPTED.getStepId().equals(business.getStepId())) {
            throw new ServiceException("当前业务步骤不允许修改房屋信息");
        }
        // 拉取基础房屋信息
        List<GwjBaseHouse> baseHouses = houseService.selectGwjBaseListByHouseCode(chooseHouseRequest.getHouseCode());
        // 判断房屋条数与选中条数是否相同
        if (chooseHouseRequest.getHouseCode().length != baseHouses.size()) {
            throw new ServiceException("业务房屋信息有误，请重新选择");
        }
        // 查询业务楼栋信息
        Map<String, Long> buildMap = gwjDepositBusinessBuildService.selectGwjDepositBusinessBuildListByBizId(business.getBizId()).stream().collect(Collectors.toMap(GwjBaseBusinessBuild::getBuildCode, GwjDepositBusinessBuild::getBuildId));
        // 处理业务房屋数据
        List<GwjDepositBusinessHouse> houses = baseHouses.stream().map(baseHouse -> {
            GwjDepositBusinessHouse house = new GwjDepositBusinessHouse();
            BeanUtil.copyProperties(baseHouse, house);
            house.setBizId(business.getBizId());
            house.setBuildId(buildMap.get(house.getBuildCode()));
            house.setDepositSize(null != house.getActualSize() && !new BigDecimal("0.00").equals(house.getActualSize()) ? house.getActualSize() : null != house.getForecastSize() && !new BigDecimal("0.00").equals(house.getForecastSize()) ? house.getForecastSize() : new BigDecimal("0.00"));
            // 默认选中模式
            house.setSelectFlag(GwjConstants.YES);
            house.setPublicFlag(GwjConstants.NO);
            house.setDepositMoney(new BigDecimal("0.00"));
            return house;
        }).collect(Collectors.toList());
        // 判断是否重复选中房屋信息
        List<GwjDepositBusinessHouse> oldHouseList = gwjDepositBusinessHouseService.selectGwjDepositBusinessHouseListByBizId(business.getBizId());
        for (GwjDepositBusinessHouse gwjDepositBusinessHouse : oldHouseList) {
            for (GwjDepositBusinessHouse house : houses) {
                if (gwjDepositBusinessHouse.getHouseId().equals(house.getHouseId())) {
                    throw new ServiceException("请勿重复选中相同房屋");
                }
            }
        }
        // 保存业务房屋信息
        if (0 == gwjDepositBusinessHouseService.insertGwjDepositBusinessHouses(houses)) {
            throw new ServiceException("业务房屋信息导入失败");
        }
        // 查询业务业务金额信息
        GwjDepositBusinessMoney money = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(business.getBizId());
        if (ObjectUtil.isEmpty(money)) {
            throw new ServiceException("未查询倒对应业务金额信息");
        }
        money.setTotalOwner(null != money.getTotalOwner() && 0L != money.getTotalOwner() ? money.getTotalOwner() - Long.valueOf(houses.size()) : Long.valueOf(houses.size()));
        // 更新业务信息
        if (1 != gwjDepositBusinessMoneyService.updateGwjDepositBusinessMoney(money)) {
            throw new ServiceException("业务金额信息更新失败");
        }

    }

    /**
     * 删除业务房屋
     * 
     * @param bizId 删除参数
     * @param bizHouseIds 业务房屋主键
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHousesData(Long bizId, Long[] bizHouseIds) {
        // 校验权限
        GwjDepositBusiness business = gwjDepositBusinessService.checkPermission(bizId);
        // 校验步骤
        if (!GwjOwnerDepositStepEnum.ACCEPTED.getStepId().equals(business.getStepId())) {
            throw new ServiceException("当前业务步骤不允许修改房屋信息");
        }
        // 查询业务房屋
        List<GwjDepositBusinessHouse> houses = gwjDepositBusinessHouseService.selectGwjDepositBusinessHouseListByBizIds(bizHouseIds);
        // 判断业务房屋条数与取消是否一致
        if (bizHouseIds.length != houses.size()) {
            throw new ServiceException("业务房屋信息有误，请重新选择");
        }
        // 计算房屋缴款金额
        BigDecimal totalDepositMoney = new BigDecimal(houses.stream().map(GwjDepositBusinessHouse::getDepositMoney).mapToDouble(BigDecimal::doubleValue).sum());
        // 查询业务金额信息
        GwjDepositBusinessMoney money = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(bizId);
        if (ObjectUtil.isEmpty(money)) {
            throw new ServiceException("未查询到对应业务金额信息");
        }
        // 处理业务金额信息
        money.setTotalMoney(null != money.getTotalMoney() && !new BigDecimal("0.00").equals(money.getTotalMoney()) ? money.getTotalMoney().subtract(totalDepositMoney) : new BigDecimal("0.00"));
        money.setTotalOwner(null != money.getTotalOwner() && 0L != money.getTotalOwner() ? money.getTotalOwner() - Long.valueOf(houses.size()) : 0L);
        // 更新业务金额信息
        if (1 != gwjDepositBusinessMoneyService.updateGwjDepositBusinessMoney(money)) {
            throw new ServiceException("业务金额信息更新失败");
        }
        // 处理业务信息
        business.setLackMoney(null != business.getLackMoney() && !new BigDecimal("0.00").equals(business.getLackMoney()) ? business.getLackMoney().subtract(totalDepositMoney) : new BigDecimal("0.00"));
        // 更新业务信息
        if (1 != gwjDepositBusinessMapper.updateGwjDepositBusiness(business)) {
            throw new ServiceException("业务信息更新失败");
        }
        // 删除业务房屋信息
        if (0 == gwjDepositBusinessHouseService.deleteGwjDepositBusinessHouseByBizHouseIds(bizHouseIds)) {
            throw new ServiceException("业务房屋删除失败");
        }
    }

    /**
     * 修改业务金额信息
     * 
     * @param moneyRequest 修改参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editMoney(GwjOwnerDepositEditMoneyRequest moneyRequest) {
        // 校验权限
        GwjDepositBusiness business = gwjDepositBusinessService.checkPermission(moneyRequest.getBizId());
        // 校验步骤
        if (!GwjOwnerDepositStepEnum.ACCEPTED.getStepId().equals(business.getStepId())) {
            throw new ServiceException("当前业务步骤不允许修改房屋信息");
        }
        // 查询业务房屋信息
        List<GwjDepositBusinessHouse> houseList = gwjDepositBusinessHouseService.selectGwjDepositBusinessHouseListByBizId(moneyRequest.getBizId());
        // 计算原本缴费金额信息
        BigDecimal oldDepositMoney = new BigDecimal(houseList.stream().map(GwjDepositBusinessHouse::getDepositMoney).mapToDouble(BigDecimal::doubleValue).sum());
        houseList = houseList.stream().map(item -> {
            item.setDepositMoney(null != item.getDepositSize() ? item.getDepositSize().multiply(new BigDecimal(moneyRequest.getCalculateTypeId())) : new BigDecimal("0.00"));
            item.setCalculateTypeId(moneyRequest.getCalculateTypeId());
            item.setCalculateVal(new BigDecimal(moneyRequest.getCalculateTypeId()));
            return item;
        }).collect(Collectors.toList());
        // 更新业务房屋信息
        if (0 != houseList.size()) {
            gwjDepositBusinessHouseService.updateBatchGwjDepositBusinessHouse(houseList);
        }
        // 计算更新后缴费金额信息
        BigDecimal newDepositMoney = new BigDecimal(houseList.stream().map(GwjDepositBusinessHouse::getDepositMoney).mapToDouble(BigDecimal::doubleValue).sum());
        // 查询业务金额信息
        GwjDepositBusinessMoney money = gwjDepositBusinessMoneyService.selectGwjDepositBusinessMoneyByBizId(moneyRequest.getBizId());
        if (ObjectUtil.isEmpty(money)) {
            throw new ServiceException("未查询到对应业务金额信息");
        }
        // 处理业务金额信息
        money.setCalculateTypeId(moneyRequest.getCalculateTypeId());
        money.setTotalMoney(null != money.getTotalMoney() && !new BigDecimal("0.00").equals(money.getTotalMoney()) ? money.getTotalMoney().subtract(oldDepositMoney).add(newDepositMoney) : newDepositMoney);
        // 更新业务信息
        if (1 != gwjDepositBusinessMoneyService.updateGwjDepositBusinessMoney(money)) {
            throw new ServiceException("业务金额信息更新失败");
        }
        // 处理业务信息
        business.setLackMoney(null != business.getLackMoney() && !new BigDecimal("0.00").equals(business.getLackMoney()) ? business.getLackMoney().subtract(oldDepositMoney).add(newDepositMoney) : newDepositMoney);
        // 更新业务信息
        if (1 != gwjDepositBusinessMapper.updateGwjDepositBusiness(business)) {
            throw new ServiceException("业务信息更新失败");
        }
    }

}
